package com.xinqing.test.LetCode;

import java.util.HashSet;

/**
 * @Author:CHQ
 * @Date:2021/7/20 16:07
 * @Description
 */
public class DetectCycle {
    public static void main(String args[]) {

        ListNode head=new ListNode(1);
        ListNode head2=new ListNode(2);
        ListNode head3=new ListNode(3);
        ListNode head4=new ListNode(4);
        head.next=head2;
        head2.next=head3;
        head3.next=head4;
        head4.next=head2;

        System.out.println(test(head));
    }
    public static int test(ListNode head){
        HashSet<ListNode> set=new HashSet<>();

        while(head!=null){
            if(!set.add(head)){
                return head.val;
            }

            set.add(head);
            head=head.next;
        }
        return -1;
    }
}


